﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace demoReflection
{
    public class Helper
    {
       
        public static Type TryGetType(string nameType, string assemlyLocation)
        {
            Type t = null;
            Assembly assameblyfile = Assembly.LoadFile(assemlyLocation);
            if (assameblyfile != null)
            {
                t = assameblyfile.GetType(nameType);
            }
            return t;
        }
        public static Type TryGetType(string nameType)
        {

            Type t = null;
            if (!string.IsNullOrWhiteSpace(nameType))
            {
                t = Type.GetType(nameType);
                if (t == null)
                {
                    foreach (Assembly a in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        //Console.WriteLine(a.ToString());
                        t = a.GetType(nameType);
                        if (t != null)
                            break;
                    }
                    if (t == null)
                    {
                        string pathLocation = Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory);

                        string[] pathfiles = Directory.GetFiles(pathLocation, "*.dll");
                        foreach (string path in pathfiles)
                        {
                            t = TryGetType(nameType, path);
                            if (t != null)
                                break;
                        }

                    }
                }
            }
            return t;
        }

        public static List<Type> TryGetTypes(string[] nameTypes)
        {
            List<Type> types = new List<Type>();
            for (int i = 0; i < nameTypes.Length; i++)
            {
                Type t = TryGetType(nameTypes[i]);
                if (t != null) {
                    types.Add(t);
                }
            }
            return types;
        }

        public static Dictionary<string, Type> GetDictionaryOfType(string filenameXML)
        {
            Dictionary<string, Type> dicType = new Dictionary<string,Type>();
            XDocument doc = XDocument.Load(filenameXML);


            return dicType;
            
        }
    }
}
